<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class AdminRequest extends FormRequest
{
    // 后台公用form请求类，重写failedValidation()，返回json数据
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    /**
     * 返回json数据
     *
     * @param Validator $validator
     */
    protected function failedValidation( Validator $validator)
    {
        throw new HttpResponseException(
            response()->json([
                'code' => 500,
                'message' => '请求数据错误',
                'msg' => $validator->errors()->first()
            ])
        );
    }
}
